package com.qlh.camera

import com.qlh.camera.view.InfiniteProgressPanel
import java.awt.*
import java.text.SimpleDateFormat
import java.util.*
import javax.swing.*
import javax.swing.table.DefaultTableCellRenderer
import javax.swing.table.DefaultTableModel
import javax.swing.tree.DefaultMutableTreeNode
import javax.swing.tree.DefaultTreeModel
import javax.swing.tree.TreePath


val tabeTile = arrayOf("时间", "成熟度", "推荐区域", "图片路径")
/*************************************************
函数:    centerWindow
函数描述:窗口置中
 *************************************************/
fun CameraClient.centerWindow() {
    val dim = Toolkit.getDefaultToolkit().screenSize
    val windowSize = (dim.height * 0.9).toInt()
    val maxW = windowSize * interfaceSetting.getWidthHeightScale().toInt()
    size = Dimension((windowSize * interfaceSetting.getWidthHeightScale()).toInt(), windowSize)
    val w = this.size.width
    val h = this.size.height
    val x = (dim.width - w) / 2
    val y = (dim.height - h) / 2
    setLocation(x, y)
    println("界面初始化完成")
}

/*************************************************
函数:    getChannelNumber
函数描述:从设备树获取通道号
 *************************************************/
fun JTree.getChannelNumber(): Int {
    var iChannelNum: Int
    val tp: TreePath = this.selectionPath //获取选中节点的路径

    iChannelNum = if (tp != null) //判断路径是否有效,即判断是否有通道被选中
    {
        //获取选中的通道名,对通道名进行分析:
        val sChannelName = (tp.lastPathComponent as DefaultMutableTreeNode).toString()
        if (sChannelName[0] == 'C') //Camara开头表示模拟通道
        { //子字符串中获取通道号
            sChannelName.substring(6).toInt()
        } else {
            if (sChannelName[0] == 'I') //IPCamara开头表示IP通道
            { //子字符创中获取通道号,IP通道号要加32
                sChannelName.substring(8).toInt() + 32
            } else {
                return -1
            }
        }
    } else {
        return -1
    }
    return iChannelNum
}

/*************************************************
函数:      initialTreeModel
函数描述:  初始化设备树
 *************************************************/
fun DefaultMutableTreeNode.initialTreeModel(): DefaultTreeModel = DefaultTreeModel(this)

/*************************************************
函数:      initialTableModel
函数描述:	初始化报警信息列表,写入列名称
 *************************************************/
fun Any.initialTableModel(): DefaultTableModel {

    return DefaultTableModel(tabeTile, 0)
}

fun JTable.setParams() = this.apply {
    rowHeight = 35
    tableHeader.apply {
        preferredSize = Dimension(width, 35)
        font = Font("Dialog", 1, 20)
//        defaultRenderer = object : DefaultTableCellRenderer() {
//            override fun getTableCellRendererComponent(table: JTable?, value: Any?, isSelected: Boolean, hasFocus: Boolean, row: Int, column: Int): Component {
//                //background = Color(168,220,231)
//                horizontalAlignment = SwingConstants.CENTER
//                return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column)
//            }
//        }
    }
//    for (i in 0 until columnCount) {
//        getColumn(tabeTile[i]).setCellRenderer(object : DefaultTableCellRenderer() {
//            override fun getTableCellRendererComponent(table: JTable?, value: Any?, isSelected: Boolean, hasFocus: Boolean, row: Int, column: Int): Component {
//                if (row % 2 == 0) {
//                    //background = Color.white
//                } else {
//                    //background = Color.white
//                }
//                horizontalAlignment = SwingConstants.CENTER
//
//                return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column)
//            }
//        })
//    }
}

//添加拍照信息
fun JTable.addInfo(mt: String, area: String, path: String, date: String? = null): String {
    val alarmTableModel = this.model as DefaultTableModel//获取表格模型
    val newRow = arrayListOf<String>()
    //时间
    val time = SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(Date())
    newRow.add(date ?: time)
    //成熟度
    newRow.add(mt)
    //区域
    newRow.add(area)
    //路径
    newRow.add(path)
    alarmTableModel.insertRow(0, newRow.toArray())
    return "$time,$mt,$area,$path\r\n"
}

fun GroupLayout.SequentialGroup.addView(c1: Component, c2: Component, b1: Int = 10, b2: Int = 14, pref: Int = 130): GroupLayout.SequentialGroup =
        this.addGap(b1, b1, b1)
                .addComponent(c1)
                .addGap(b2, b2, b2) //间隔空白大小 GroupLayout.PREFERRED_SIZE, pref, GroupLayout.PREFERRED_SIZE
                .addComponent(c2)
                .addGap(b1, b1, b1)

fun GroupLayout.SequentialGroup.addView(jPanelUserInfoLayout: GroupLayout, c1: Component, c2: Component): GroupLayout.SequentialGroup =
        this.addGroup(jPanelUserInfoLayout.createParallelGroup(GroupLayout.Alignment.CENTER)
                .addComponent(c1, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
                //GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE
                .addComponent(c2, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        )

fun GroupLayout.SequentialGroup.addView(jPanelUserInfoLayout: GroupLayout, c1: Component, c2: Component, c3: Component): GroupLayout.SequentialGroup =
        this.addGroup(jPanelUserInfoLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(c1)
                .addComponent(c2)
                .addComponent(c3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        )


fun CameraClient.startLoading(msg: String = "处理中..."): InfiniteProgressPanel {
    val glasspane = InfiniteProgressPanel()
    val dim = Toolkit.getDefaultToolkit().screenSize
    val maxw = interfaceSetting.getUserInfoDividerPosition()
    glasspane.setBounds(maxw / 2, height / 2, dim.width / 4, dim.height)
    glassPane = glasspane
    glasspane.text = msg
    glasspane.start()//开始动画加载效果
    return glasspane
}
